<?

include_once "rbns.php5";

//
// and RBNS main visible class




class RBNS_engine extends RBNS{
// template parser class definition


 
  protected  $usertest=false;//check singleton user

  protected  $precompiled=false; // flag for check type of template
  protected  $rules=''; // class-group rules string(32)
  protected  $rulesData=''; // data for rules
  /**
   * If usertest=true it is reference of $GLOBALS['_RBNS_SINGLETON'] which should be RBNS_user class's object
   *
   * @var RBNS_user
   */
  protected  $user=null;
  /**
   * Cache for RBNS_user->user->getId();
   * Present only if $this->usertest=true;
   *
   * @var int
   */
  public  $id_user;
  
  
  /**
   * Current version of template
   *
   * @var string
   */
  protected  $_VERSION_OF_TEMPLATE=''; 
  
  

/**
 * Constructor 
 * If usertest property set to true trying make
 * refence for user property to $GLOBALS['_RBNS_SINGLETON'] which should be RBNS_user class's object
 *
 */
function __construct() {
  // class constructor
  
   
   if($this->usertest)
   {
        if(!defined("_RBNS_SINGLETON"))
        {
            $this->errorstream('notDefinedRequredConstant_RBNS_SINGLETON','critical');
            die();
        };
        
        if(!is_object($GLOBALS[_RBNS_SINGLETON]))
        {
           $this->errorstream('_RBNS_SINGLETON is not object','critical');
           die();
        };
        $this->user=&$GLOBALS[_RBNS_SINGLETON];
        $this->id_user=$this->user->getId();
   };

   parent::__construct();
}



/**
 * Set _VERSION_OF_TEMPLATE
 *
 * @param string $ver
 * @return boolean
 */
function setTemplateVersion($ver){
    if(empty($ver)) return false;
    $this->_VERSION_OF_TEMPLATE=$ver;
    return true;
}

/**
 * get current _VERSION_OF_TEMPLATE
 *
 * @return unknown
 */
function getTemplateVersion(){
    return $this->_VERSION_OF_TEMPLATE;
}


protected $aAspects=array(); 

protected $tAspects=array();
protected function setTemplateAspects($class,$method,$version){
	$this->tAspects=array();  
	if(isset($this->aAspects[$class]) && isset($this->aAspects[$class][$method]) && isset($this->aAspects[$class][$method][$version]) ){
	 	$this->tAspects=$this->aAspects[$class][$method][$version]; 
	}
	
	
	
	
}

protected function runAspect($tag,$sit,&$data=null){
	if(!empty($this->tAspects) && is_array($this->tAspects)){
		
		if(isset($this->tAspects[$tag]) && isset($this->tAspects[$tag][$sit]) && is_array($this->tAspects[$tag][$sit])){
			$run=&$this->tAspects[$tag][$sit];
			foreach($this->tAspects[$tag][$sit] as $method){
				$this->$method($data);
			}
		}
	}
}

protected function setAspect2Method($method,$version,$ar){
	if(is_array($ar)){
		$this->aAspects[get_class($this)][$method][$version]=$ar;
	}
}


/**
 * Return early compiled template code for method of class with current version.
 * If there is not code for current version trying return compiled code
 * for ancestor of this class.
 * If not version change to 'default' and cycle repeating.
 * 
 * 
 *
 * @param string $method  name of method
 * @return  false - if fault and generate RBNS_USER_ERROR; code if success.
 */
function getTemplate($method){
    global $_RBNS_VERSION_OF_TEMPLATE,$RBNS_compiled_templates;


    $this->precompiled=false;

    if( (!($_VOF=$this->_VERSION_OF_TEMPLATE))&&
        (!($_VOF=$_RBNS_VERSION_OF_TEMPLATE)) ) $_VOF='default';



    $i=0;
    do{
        $classname=get_class($this);
        
        if($i){
      
	   // second iteration without version cheking

            $oVOF=$_VOF; $_VOF='default';
            $RBNS_compiled_templates[$classname][$method][$oVOF]=&$RBNS_compiled_templates[$classname][$method][$_VOF]; //for caching
        };
        $i++;
		
        if (isset($RBNS_compiled_templates[$classname][$method][$_VOF])){
         //try get from cache
                $this->precompiled=true;
                $this->setTemplateAspects($classname,$method,$_VOF);
                return  $RBNS_compiled_templates[$classname][$method][$_VOF];
        };
     	
        $classname=get_class($this);
        while((!file_exists($this->makeTemplateFileName($classname,$method,$_VOF) )) && $classname){
        
		// if there is not file for current version check parent class fot it		
    	    $classname=get_parent_class($classname);
    	};

        if($classname && file_exists( $filename=$this->makeTemplateFileName($classname,$method,$_VOF) ) ){
		 // found	
            $s='';
		 	$f=fopen($filename,'r') or die("failed open file $filename");
    	    while(!feof($f)) {$s.=fread($f,4096);}; fclose($f);
    	    $this->precompiled=true;
    	    $this->setTemplateAspects($classname,$method,$_VOF);
    	    return $RBNS_compiled_templates[$classname][$method][$_VOF]=$s;

        };

    }while($_VOF!='default');

   
    $this->errorstream('notFoundTemplate',RBNS_USER_ERROR,"class '".get_class($this)."' method '$method' ");
    return false;
}


/**
 * Get Rules for pair class,method from user and
 * set it to rulesData and  rules
 *
 * @param string $method name of method
 * @return boolean
 */
function getRule($method=''){
    
	try {
		$a=$this->user->getRules(get_class($this),$method);

		if (!is_array($a)){
			$this->rulesData='';
			$this->rules='';
			return false;
		}
		else{
			$this->rulesData=$a['data'];
			$this->rules=$a['rules'];
			return true;
		}
	} catch (Exception $e){
		return false;
	}
}

/**
 * Is user have administrators rights
 *
 * @return boolean,null
 */
function isAdmin(){
 	try{
		return $this->user->isadmin();
 	}catch (Exception $e){
 		return null;
 	}
}






}

?>